<?php

namespace app\shopping\controller;

use think\Controller;
/**
 * 收货地址接口
 */
class Address extends Controller
{
    public function __construct(){
        parent::__construct();

        $this->BusinessModel = model('Business.User');
        $this->AddressModel = model('Business.Address');

        $busid = $this->request->param('busid',0,'trim');
        $this->business = $this->BusinessModel->where(['id' => $busid])->find();
        if(!$this->business){
            $this->error('用户不存在');
            exit;
        }
    }

    /**
     * 收货地址列表
     *
     * @ApiTitle    (收货地址列表)
     * @ApiSummary  (收货地址列表)
     * @ApiMethod   (POST)
     * @ApiParams   (name="busid", type="int", required=true, description="客户ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function index(){
        
        if($this->request->isPost()){
            $address = $this->AddressModel->where(['busid'=>$this->business['id']])->select();
            
            if($address){
                $this->success('查询收货地址',null,$address);
                exit;
            }else{
                $this->error('暂无地址');
                exit;
            }
        }
    }

    /**
     * 新增收货地址
     *
     * @ApiTitle    (新增收货地址)
     * @ApiSummary  (新增收货地址)
     * @ApiMethod   (POST)
     * @ApiParams   (name="busid", type="int", required=true, description="客户ID")
     * @ApiParams   (name="consignee", type="string", required=true, description="收货人名称")
     * @ApiParams   (name="code", type="string", required=true, description="地区码")
     * @ApiParams   (name="address", type="string", required=true, description="详细地址")
     * @ApiParams   (name="mobile", type="string", required=true, description="客户电话")
     * @ApiParams   (name="status", type="boolen", required=true, description="是否为默认地址")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function add(){
        if($this->request->isPost()){
            $params = $this->request->param();
            // var_dump($params);
            // exit;
            $code = $this->request->param('code','','trim');
            $status = $this->request->param('status','0','trim');

            // 判断是否有地区数据
            if(!empty($code)){
                // 查询省市区地区码
                $parent = model('Region')->where(['code' => $code])->value('parentpath');

                if(!empty($parent)){
                    $arr = explode(',',$parent);
                    $params['province'] = isset($arr[0]) ? $arr[0] : null;
                    $params['city'] = isset($arr[1]) ? $arr[1] : null;
                    $params['district'] = isset($arr[2]) ? $arr[2] : null;
                }
            }
            // 开启事务
            $this->AddressModel->startTrans();

            // 判断是否选择了默认地址
            if($status == '1'){
                // 直接更新覆盖，将已有的变成0
                $AddressStatus = $this->AddressModel->where(['busid'=>$this->business['id']])->update(['status'=>'0']);

                if($AddressStatus === FALSE){
                    $this->error('更新默认地址失败');
                    exit;
                }
            }
            // var_dump($params);
            // exit;
            // 插入数据
            $result = $this->AddressModel->validate('common/Business/Address')->save($params);
            
            if($result === FALSE){
                $this->AddressModel->rollback();
                $this->error($this->AddressModel->getError());
                exit;
            }else{
                $this->AddressModel->commit();
                $this->success('添加成功');
                exit;
            }
        }
    }


    /**
     * 收货地址信息
     *
     * @ApiTitle    (收货地址信息)
     * @ApiSummary  (收货地址信息)
     * @ApiMethod   (POST)
     * @ApiParams   (name="id", type="int", required=true, description="收货地址ID")
     * @ApiParams   (name="busid", type="int", required=true, description="客户ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function info(){
        if($this->request->isPost()){
            $id = $this->request->param('id',0,'trim');
            // var_dump($id);
            // exit;
            $where = [
                'id' => $id,
                'busid' => $this->business['id']
            ];

            $address = $this->AddressModel->where($where)->find();

            if($address){
                $this->success('返回收货地址',null,$address);
                exit;
            }else{
                $this->error('地址不存在');
                exit;
            }
        }
    }

    /**
     *(编辑收货地址)
     *
     * @ApiTitle    (编辑收货地址)
     * @ApiSummary  (编辑收货地址)
     * @ApiMethod   (POST)
     * @ApiParams   (name="busid", type="int", required=true, description="客户ID")
     * @ApiParams   (name="consignee", type="string", required=true, description="收货人名称")
     * @ApiParams   (name="code", type="string", required=true, description="地区码")
     * @ApiParams   (name="address", type="string", required=true, description="详细地址")
     * @ApiParams   (name="mobile", type="string", required=true, description="客户电话")
     * @ApiParams   (name="status", type="boolen", required=true, description="是否为默认地址")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function edit(){
        if($this->request->isPost()){
            $params = $this->request->param();
            // var_dump($params);
            // exit;
            $id = $this->request->param('id',0,'trim');
            $code = $this->request->param('code','','trim');
            $status = $this->request->param('status','0','trim');

            $where = [
                'id' => $id,
                'busid' => $this->business['id']
            ];

            $address = $this->AddressModel->where($where)->find();

            if(!$address){
                $this->error('收货地址不存在');
                exit;
            }

            // 判断是否有地区数据
            if(!empty($code)){
                // 查询省市区地区码
                $parent = model('Region')->where(['code' => $code])->value('parentpath');

                if(!empty($parent)){
                    $arr = explode(',',$parent);
                    $params['province'] = isset($arr[0]) ? $arr[0] : null;
                    $params['city'] = isset($arr[1]) ? $arr[1] : null;
                    $params['district'] = isset($arr[2]) ? $arr[2] : null;
                }
            }
            // 开启事务
            $this->AddressModel->startTrans();

            // 判断是否选择了默认地址
            if($status == '1'){
                // 直接更新覆盖，将已有的变成0
                $AddressStatus = $this->AddressModel->where(['busid'=>$this->business['id']])->update(['status'=>'0']);

                if($AddressStatus === FALSE){
                    $this->error('更新默认地址失败');
                    exit;
                }
            }
            // var_dump($params);
            // exit;
            // 插入数据
            $result = $this->AddressModel->validate('common/Business/Address')->isUpdate(true)->save($params);
            
            if($result === FALSE){
                $this->AddressModel->rollback();
                $this->error($this->AddressModel->getError());
                exit;
            }else{
                $this->AddressModel->commit();
                $this->success('更新成功');
                exit;
            }
        }
    }

    /**
     * 删除收货地址
     *
     * @ApiTitle    (删除收货地址)
     * @ApiSummary  (删除收货地址)
     * @ApiMethod   (POST)
     * @ApiParams   (name="id", type="int", required=true, description="收货地址ID")
     * @ApiParams   (name="busid", type="int", required=true, description="客户ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function del(){
        if($this->request->isPost()){
            $params = $this->request->param();

            $id = $this->request->param('id',0,'trim');
            // var_dump($id);
            // exit;
            $where = [
                'id' => $id,
                'busid' => $this->business['id']
            ];

            $address = $this->AddressModel->where($where)->find();

            if(!$address){
                $this->error('收货地址不存在');
                exit;
            }

            $result = $this->AddressModel->destroy($id);

            if($result === FALSE){
                $this->error('删除失败');
                exit;
            }else{
                $this->success('删除成功');
                exit;
            }
        }
    }
}
